package leetcode;

import java.util.Arrays;

/**
 * 16. 最接近的三数之和
 * https://leetcode-cn.com/problems/3sum-closest/description/
 */
public class ThreeSumClosest {

    public int threeSumClosest(int[] nums, int target){
        int res = nums[0] + nums[1] + nums[2];
        int minDist = Math.abs(res - target);
        int len = nums.length;
        for(int i = 0;i < len - 2; i++){
            for(int j = i + 1; j < len - 1; j++){
                for(int k = j + 1; k < len; k++){
                    int sum = nums[i] + nums[j] + nums[k];
                    int dist = Math.abs(sum - target);
                    if(dist < minDist){
                        minDist = dist;
                        res = sum;
                    }
                }
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] nums = {0, 2, 1, -3};
        new ThreeSumClosest().threeSumClosest(nums, 1);
    }

}
